package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;

import java.util.ArrayList;

import com.example.administrator.szb.activity.base.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.fragments.FragmentGuWenLogin;
import com.example.administrator.szb.fragments.FragmentUserLogin;

/**
 *该登录方式不用了
 */
public class LoginActivity extends BaseActivity {

    private FragmentUserLogin fragmentUserLogin;
    private FragmentGuWenLogin fragmentGuWenLogin;
    private FragmentManager fragmentManager;
    private ArrayList<Fragment> fragmentArrayList;
    private FragmentTransaction fragmentTransaction;

    @Bind(R.id.tool_bar)
    Toolbar toolBar;
    @Bind(R.id.tv_user)
    TextView tvUser;
    @Bind(R.id.tv_jrgwuser)
    TextView tvJrgwuser;
    @Bind(R.id.login_model_content)
    FrameLayout loginModelContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initFragment();
        SampleApplicationLike.getActivitiesInstance().add(this);
    }

    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        fragmentArrayList = new ArrayList<Fragment>();
        setModelLogin(0);
    }

    @OnClick({R.id.tv_user, R.id.tv_jrgwuser})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_user:
                setModelLogin(0);
                break;
            case R.id.tv_jrgwuser:
                setModelLogin(1);
                break;
        }
    }

    private void setModelLogin(int i) {
        resetStatu();//重置状态
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (i){
            case 0:
                changeStatu(i);
                if(fragmentUserLogin == null){
                    fragmentUserLogin = new FragmentUserLogin();
                    changeFragment(fragmentUserLogin);
                }else{
                    fragmentTransaction.show(fragmentUserLogin);
                }
                break;
            case 1:
                changeStatu(i);
                if(fragmentGuWenLogin == null){
                    fragmentGuWenLogin = new FragmentGuWenLogin();
                    changeFragment(fragmentGuWenLogin);
                }else{
                    fragmentTransaction.show(fragmentGuWenLogin);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
        
    }

    /**
     * 切换fragment
     * @param fragment
     */
    private void changeFragment(Fragment fragment) {
        fragmentArrayList.add(fragment);
        fragmentTransaction.add(R.id.login_model_content,fragment);
        fragmentTransaction.show(fragment);
    }

    /**
     * 切换fragment后修改组件状态
     */
    private void changeStatu(int i) {
        switch (i){
            case 0:
                tvUser.getBackground().setAlpha(255);
                tvUser.setBackgroundResource(R.drawable.white_shixinbg_roundcorner);
                tvUser.setTextColor(getResources().getColor(R.color.colorPrimary));

                tvJrgwuser.getBackground().setAlpha(0);
                tvJrgwuser.setTextColor(getResources().getColor(R.color.white));

                break;
            case 1:
                tvJrgwuser.getBackground().setAlpha(255);
                tvJrgwuser.setBackgroundResource(R.drawable.white_shixinbg_roundcorner);
                tvJrgwuser.setTextColor(getResources().getColor(R.color.colorPrimary));

                tvUser.getBackground().setAlpha(0);
                tvUser.setTextColor(getResources().getColor(R.color.white));
                break;
            default:
                break;
        }
    }

    /**
     * 隐藏所有的fragment
     * @param fragmentTransaction
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment: fragmentArrayList) {
            fragmentTransaction.hide(fragment);
        }
    }

    /**
     * 重置切换组件的状态
     */
    private void resetStatu() {
        tvUser.setTextColor(getResources().getColor(R.color.white));
        tvUser.getBackground().setAlpha(0);


        tvJrgwuser.getBackground().setAlpha(0);
        tvJrgwuser.setTextColor(getResources().getColor(R.color.white));
    }
}
